package com.smh.aiagent.tools;

import lombok.AllArgsConstructor;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

/**
 * @author shiminghui
 * @date 2025/6/5 20:00
 * @description: 邮件发送工具类
 */
@AllArgsConstructor
public class SendEmailTool {

    private final JavaMailSender mailSender;
    private final String from;

    @Tool(description = "Send an email to the specified recipient")
    public String sendEmail(
            @ToolParam(description = "Recipient's email address") String to,
            @ToolParam(description = "The subject of the message") String subject,
            @ToolParam(description = "The content of the message") String content,
            ToolContext toolContext) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);
            mailSender.send(message);
            return "The email was sent successfully";
        } catch (Exception e) {
            return "The message failed to be sent: " + e.getMessage();
        }
    }

    @Tool(description = "Send a message with a copy to the specified recipient")
    public String sendEmailWithCC(
            @ToolParam(description = "Recipient's email address") String to,
            @ToolParam(description = "The email address of the person who sent the CC") String cc,
            @ToolParam(description = "The subject of the message") String subject,
            @ToolParam(description = "The content of the message") String content,
            ToolContext toolContext) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setCc(cc);
            message.setSubject(subject);
            message.setText(content);
            mailSender.send(message);
            return "The email with a CC was sent successfully";
        } catch (Exception e) {
            return "The message failed to be sent: " + e.getMessage();
        }
    }
}
